from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time
class Base():
    # 初始化驱动
    def __init__(self,driver):
        self.driver=driver
    # 打开浏览器
    def open_url(self,url):
        self.driver.get(url)

    # 封装系统时间与截图方法
    def time(self,name):
        time_eyeny=time.strftime("%Y%m%d%H%M%S",time.localtime())
        self.driver.save_sreenhost(time_eyeny+name)
    # 封装异常截图与隐式等待的方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver : driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
            self.time('jiuxian.png')
    # 封装点击事件的方法
    def click(self,loc):
        self.find_element(loc).click()
    # 封装键盘输入的方法
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)

    # 键值下拉框事件
    def Select(self,loc,text):
        s=Select(self.find_element(loc))
        s.select_by_value(text)
    def Select2(self,loc,text):
        s=Select(self.find_element(loc))
        s.select_by_index(text)
    def Select3(self,loc,text):
        s=Select(self.find_element(loc))
        s.select_by_visible_text(text)
    # 悬停鼠标事件
    def move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(loc).perform()
    # 右键鼠标事件
    def context_click(self,loc):
        ActionChains(self.driver).context_click(loc).perform()
    # 双击鼠标事件
    def double_click(self,loc):
        ActionChains(self.driver).double_click(loc).perform()

    # 键盘回车事件
    def hui_keys(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    # 键盘Ctrl+1事件
    def Ctrl1_keys(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL+'c')
    # 键盘Ctrl+2
    def Ctrl2_keys(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL+'v')
    # 键盘SHIFI+'1'
    def Ctrl3_keys(self,loc):
        self.find_element(loc).send_keys(Keys.SHIFT+'1')